package root.electronclass.electronclass.home.widget;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import root.electronclass.electronclass.common.base.BasicAdapter;
import root.electronclass.electronclass.common.base.BindingHolder;
import root.electronclass.electronclass.mvp.GlobalParameter;
import root.electronclass.electronclass.mvp.base.BaseResponse;
import root.electronclass.electronclass.mvp.entity.HomeWorkInfoH;
import root.electronclass.electronclass.mvp.rest.RestManager;
import root.electronclass.electronclass.MainActivity;
import root.electronclass.electronclass.R;
import root.electronclass.electronclass.databinding.HomeHomeworkInfoBinding;
import root.electronclass.electronclass.databinding.HomeItemHomeworkBinding;
import root.electronclass.electronclass.grade.homework.ClassHomeworkFragment;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class HomeWorkView {
    private static final String TAG = "HomeWorkView";

    private Context context;
    private HomeHomeworkInfoBinding binding;


    public HomeWorkView(Context context, ViewGroup view) {
        this.binding = HomeHomeworkInfoBinding.bind(view);
        this.context = context;
        init();
    }

    private BasicAdapter<HomeWorkInfoH, HomeItemHomeworkBinding> adapter = new BasicAdapter<HomeWorkInfoH, HomeItemHomeworkBinding>() {
        @Override
        public void onBindViewHolder(@NonNull BindingHolder<HomeItemHomeworkBinding> holder, @Nullable HomeWorkInfoH item) {
            holder.getBinding().tvName.setText(item.getSubjectName());
            holder.getBinding().tvTime.setText("截止时间："+item.getEndTime());
            if (item.getStatus() == 0) {
                holder.getBinding().tvState.setText("未发布");
                holder.getBinding().tvState.setTextColor(Color.parseColor("#FF503D"));
                holder.getBinding().tvState.setBackground(context.getDrawable(R.drawable.bg_fe401f_item));
            } else {
                holder.getBinding().tvState.setText("已发布");
                holder.getBinding().tvState.setTextColor(Color.parseColor("#21D976"));
                holder.getBinding().tvState.setBackground(context.getDrawable(R.drawable.bg_21d976_item));
            }

        }

        @Override
        public void onBindViewHolder(@NonNull BindingHolder<HomeItemHomeworkBinding> holder, int position) {
            super.onBindViewHolder(holder, position);

        }
    };

    private void init() {
        binding.rcvHomeworkList.setVisibility(View.GONE);
        binding.tvMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity fragmentActivity = ((MainActivity)context);
                Bundle bundle = new Bundle();
                bundle.putSerializable("TARGET_CLASS", ClassHomeworkFragment.class);
                fragmentActivity.selectPage(MainActivity.Page.CLASS, bundle);
            }
        });
        request();
    }

    public void refresh() {
        request();
    }

    public void request() {
        long id = GlobalParameter.getEcardInfo().getClassId();
        RestManager.getInstance().initRest().homeWorks(String.valueOf(id)).enqueue(new Callback<BaseResponse<List<HomeWorkInfoH>>>() {
            @Override
            public void onResponse(Call<BaseResponse<List<HomeWorkInfoH>>> call, Response<BaseResponse<List<HomeWorkInfoH>>> response) {
                if (response.isSuccessful()) {
                    binding.rcvHomeworkList.setVisibility(View.VISIBLE);
                    LinearLayoutManager manager = new GridLayoutManager(context, 2);
                    binding.rcvHomeworkList.setLayoutManager(manager);
                    adapter.replaceAll(response.body().getRows());
                    binding.rcvHomeworkList.setAdapter(adapter);
                }
            }

            @Override
            public void onFailure(Call<BaseResponse<List<HomeWorkInfoH>>> call, Throwable t) {

            }
        });
    }


}
